package panel;

import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Transparency;

import javax.swing.ImageIcon;
import javax.swing.JButton;

/**
 * Provides application wide utilities for:
 * <ul>
 * 		<li> Creating rollover buttons that use semi-transparent images for the default state </li>
 * </ul>
 * @author Shahriar Haque
 *
 */
public class Utilities {
	
	private static final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
	private static final GraphicsDevice gs = ge.getDefaultScreenDevice();
	private static GraphicsConfiguration gc = gs.getDefaultConfiguration();
	
	/**
	 * Given an image, returns a JButton that has a semi-transparent (0.5 alpha) version of the image as the default
	 * state, 1.0 alpha version for the mouse over state and (1,1) translated version as the pressed state.
	 * @param imageRollover
	 * @return
	 */
	public static JButton getRolloverButton(ImageIcon imageRollover){
		
		int w = imageRollover.getIconWidth();
		int h = imageRollover.getIconHeight();

		//make translucent default image
		Image image = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
		Graphics2D g = (Graphics2D)image.getGraphics();
		Composite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f);
		g.setComposite(alpha);
		g.drawImage(imageRollover.getImage(), 0,0, null);
		g.dispose();
		ImageIcon iconDefault = new ImageIcon(image);

		//make a pressed image
		image = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
		g = (Graphics2D)image.getGraphics();
		g.drawImage(imageRollover.getImage(),1,1, null);
		g.dispose();
		ImageIcon iconPressed = new ImageIcon(image);

		//create the button
		JButton button = new JButton();
		//button.addActionListener(this);
		button.setIgnoreRepaint(true);

		button.setBorder(null);
		button.setContentAreaFilled(false);
		//set the button for different mouse state
		button.setIcon(iconDefault);
		button.setRolloverIcon(imageRollover);
		button.setPressedIcon(iconPressed);

		button.setPreferredSize(new Dimension(45,45));

		return button;
		
	}

}
